package com.xinli.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinli.business.domain.*;
import com.xinli.business.mapper.LfPostMapper;
import com.xinli.business.service.ILfCommentService;
import com.xinli.business.service.ILfCommentThumbsService;
import com.xinli.business.service.ILfPostFabulousService;
import com.xinli.business.service.ILfPostService;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.SecurityUtils;
import com.xinli.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 帖子Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-26
 */
@Service
public class LfPostServiceImpl extends ServiceImpl<LfPostMapper, LfPost> implements ILfPostService {
    @Autowired
    private LfPostMapper lfPostMapper;

    @Autowired
    private ILfCommentService lfCommentService;

    /**
     * 查询帖子
     *
     * @param id 帖子主键
     * @return 帖子
     */
    @Override
    public LfPost selectLfPostById(Long id) {
        return lfPostMapper.selectLfPostById(id);
    }

    /**
     * 查询帖子列表
     *
     * @param lfPost 帖子
     * @return 帖子
     */
    @Override
    public List<LfPost> selectLfPostList(LfPost lfPost) {
        return lfPostMapper.selectLfPostList(lfPost);
    }

    /**
     * 新增帖子
     *
     * @param lfPost 帖子
     * @return 结果
     */
    @Override
    public int insertLfPost(LfPost lfPost) {
        lfPost.setCreateTime(DateUtils.getNowDate());
        return lfPostMapper.insertLfPost(lfPost);
    }

    /**
     * 修改帖子
     *
     * @param lfPost 帖子
     * @return 结果
     */
    @Override
    public int updateLfPost(LfPost lfPost) {
        return lfPostMapper.updateLfPost(lfPost);
    }

    /**
     * 批量删除帖子
     *
     * @param ids 需要删除的帖子主键
     * @return 结果
     */
    @Override
    public int deleteLfPostByIds(Long[] ids) {
        return lfPostMapper.deleteLfPostByIds(ids);
    }

    /**
     * 删除帖子信息
     *
     * @param id 帖子主键
     * @return 结果
     */
    @Override
    public int deleteLfPostById(Long id) {
        return lfPostMapper.deleteLfPostById(id);
    }


    @Autowired
    private ILfPostFabulousService lfPostFabulousService;
    @Autowired
    private ILfCommentThumbsService commentThumbsService;


    @Override
    public void setViewAndReplyCountAndReList(LfPost post,String  zxsId) {
        Long postId = post.getId();
        LfComment reply = new LfComment();
        reply.setPostId(post.getId());
        reply.setPid(Long.valueOf(0));
        if(!StringUtils.isEmpty(zxsId)){
            reply.setUid(Long.valueOf(zxsId));
        }
        List<LfComment> lfComments = lfCommentService.selectLfCommentList(reply);
        for (int i = 0; i < lfComments.size(); i++) {
            LfComment lfComment = lfComments.get(i);
            QueryWrapper<LfComment> child = new QueryWrapper<LfComment>();
            child.eq("post_id", post.getId());
            child.eq("pid", lfComment.getId());
            long count = lfCommentService.count(child);
            lfComments.get(i).setPlCount(count);
            // 点赞数量
            QueryWrapper<LfCommentThumbs> queryWrapper = new QueryWrapper<LfCommentThumbs>();
            queryWrapper.eq("c_id", lfComment.getId());
            long thumbsCount = commentThumbsService.count(queryWrapper);
            lfComments.get(i).setDzCount(thumbsCount);
            Long uid = SecurityUtils.getUserId();
            QueryWrapper<LfCommentThumbs> queryWrapper1 = new QueryWrapper<LfCommentThumbs>();
            queryWrapper1.eq("c_id", lfComment.getId());
            queryWrapper1.eq("uid", uid);
            long count1 = commentThumbsService.count(queryWrapper1);
            if (count1 > 0) {
                lfComments.get(i).setIsDz(true);
            } else {
                lfComments.get(i).setIsDz(false);
            }

        }
        post.setReplyList(lfComments);
        // 评论数量
        post.setPlCount(Long.valueOf(lfComments.size()));
        long count = lfPostFabulousService.getFollowCount(postId);
        // 判断是否登录
        if (SecurityUtils.isLogin()) {
            Boolean follow = lfPostFabulousService.isFollow(postId);
            post.setIsFollow(follow);
        } else {
            post.setIsFollow(null);
        }
        post.setFollowCount(count);
    }

    @Override
    public void setViewAndReplyCount(LfPost post) {
        Long postId = post.getId();
        QueryWrapper<LfComment> reply = new QueryWrapper<>();
        reply.eq("post_id", postId);
        reply.eq("pid", 0);
        reply.eq("type", 1);
        long replycount = lfCommentService.count(reply);
        // 评论数量
        post.setPlCount(Long.valueOf(replycount));
        long count = lfPostFabulousService.getFollowCount(postId);
        Boolean follow = lfPostFabulousService.isFollow(postId);
        post.setFollowCount(count);
        post.setIsFollow(follow);
    }


    @Override
    public void updateViewCount(LfPost post) {
        // 观看数量加1
        UpdateWrapper<LfPost> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", post.getId());
        updateWrapper.setSql("read_count = read_count + 1");
        this.update(updateWrapper);

    }


}
